package com.admission.web.admin;

import java.io.File;
import java.util.List;

import org.apache.commons.io.FileUtils;
import org.apache.commons.lang.StringUtils;
import org.apache.struts2.ServletActionContext;
import org.apache.struts2.convention.annotation.Namespace;
import org.springframework.beans.factory.annotation.Autowired;
import org.springside.modules.orm.Page;
import org.springside.modules.orm.PropertyFilter;
import org.springside.modules.utils.web.struts2.Struts2Utils;

import com.admission.model.CollegeForm;
import com.admission.service.CollegeDetailManager;
import com.admission.service.CollegeFormManager;
import com.admission.util.NewFileName;
import com.admission.util.PageUtils;
import com.admission.web.CrudActionSupport;
@Namespace("/admin")
public class CollegeFormAdminAction extends CrudActionSupport<CollegeForm>{

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private CollegeFormManager collegeFormManager;
	private String id;
	private String ids;
	private CollegeForm entity;
	private Page<CollegeForm> page = new Page<CollegeForm>(PageUtils.DEFAULT_PAGE_SIZE);
	private File collegeFile;
	private String collegeFileFileName;
	private CollegeDetailManager collegeDetailManager;
	private String keywords="";
	private String status;//开闭状态

	public CollegeForm getModel() {
		return entity;
	}

	@Override
	public String list() throws Exception {
		List<PropertyFilter> filters = PropertyFilter
				.buildFromHttpRequest(Struts2Utils.getRequest());
//		if(keywords!=""){
//			PropertyFilter p=new PropertyFilter("LIKES_title", keywords);
//			filters.add(p);
//		}
		page.setOrder(Page.DESC);
		page.setOrderBy("inputTime");
		page = collegeFormManager.getPage(page, filters);
		return SUCCESS;
	}

	@Override
	public String input() throws Exception {
		return INPUT;
	}

	@Override
	public String save() throws Exception {
		
		String uploadPath = ServletActionContext.getServletContext()
				.getRealPath("/upload"); 
		String subPath = null;
		try{
			if (collegeFile != null) {
				subPath = new String();
					subPath = NewFileName.genNewFileName(
							collegeFileFileName, 0);
					FileUtils.copyFile(collegeFile, new File(uploadPath,
							subPath));
					entity.setTitle(collegeFileFileName.substring(0, collegeFileFileName.lastIndexOf(".")));
					collegeFormManager.save(entity,subPath);
					collegeDetailManager.save(uploadPath + File.separator
							+ subPath,entity.getId());
				}
		} catch (Exception e) {
			collegeFormManager.deleteByIds(entity.getId());
			   this.addActionMessage("请按照高考录取模板表进行编辑，详细错误信息可在日志管理处查看，如果没有权限可联系超级管理员");
			   return INPUT;
	    }
		
		return list();
	}

	@Override
	public String delete() throws Exception {
		try{
			collegeFormManager.deleteByIds(ids);
			 //this.addActionMessage("操作成功");
		   } catch (Exception e) {
			   this.addActionMessage("操作失败，请重新操作");
		   }  
		return list();
	}

	
	/**
	 * 改变开放状态
	 * @return
	 * @throws Exception
	 */
	public String changeConsult() throws Exception{
			collegeFormManager.changeConsult(status);
			if(status.equals("1"))
				Struts2Utils.renderText("true");
			else
				Struts2Utils.renderText("false");
			return null;
	}
	
	@Override
	protected void prepareModel() throws Exception {
		if(StringUtils.isEmpty(id))
			entity=new CollegeForm();
		else
			entity=collegeFormManager.getById(id);
		
	}

	public CollegeFormManager getCollegeFormManager() {
		return collegeFormManager;
	}
	@Autowired
	public void setCollegeFormManager(CollegeFormManager collegeFormManager) {
		this.collegeFormManager = collegeFormManager;
	}

	public String getId() {
		return id;
	}

	public void setId(String id) {
		this.id = id;
	}

	public String getIds() {
		return ids;
	}

	public void setIds(String ids) {
		this.ids = ids;
	}

	public CollegeForm getEntity() {
		return entity;
	}

	public void setEntity(CollegeForm entity) {
		this.entity = entity;
	}

	public Page<CollegeForm> getPage() {
		return page;
	}

	public void setPage(Page<CollegeForm> page) {
		this.page = page;
	}

	

	public CollegeDetailManager getCollegeDetailManager() {
		return collegeDetailManager;
	}
	@Autowired
	public void setCollegeDetailManager(CollegeDetailManager collegeDetailManager) {
		this.collegeDetailManager = collegeDetailManager;
	}

	public File getCollegeFile() {
		return collegeFile;
	}

	public void setCollegeFile(File collegeFile) {
		this.collegeFile = collegeFile;
	}

	public String getCollegeFileFileName() {
		return collegeFileFileName;
	}

	public void setCollegeFileFileName(String collegeFileFileName) {
		this.collegeFileFileName = collegeFileFileName;
	}

	public String getKeywords() {
		return keywords;
	}

	public void setKeywords(String keywords) {
		this.keywords = keywords;
	}

	public String getStatus() {
		return status;
	}

	public void setStatus(String status) {
		this.status = status;
	}

}
